from qt_core import *

style = '''
QTreeWidget  {{
	background-color: {_bg_color};
	border-radius: {_radius}px;
	border: {_border_size}px solid transparent;
	padding-left: 10px;
    padding-right: 10px;
	selection-color: {_selection_color};
	selection-background-color: {_context_color};
    color: {_color};
    {_font};
}}
QTreeWidget:branch  {{
    background: {_bg_color_active};
}}
'''
style1 = """
QTreeWidget:branch  {{
    background: {_bg_color_active};
}}
QTreeWidget:item:hover   {{
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e7effd, stop: 1 #cbdaf1);
    border: 1px solid #bfcde4;
}}
"""


class TreeWidgetItem:
    def __init__(self, id: any, parent_id: any, name: str, icon: QIcon = None, extend: object = None):
        """
        菜单数据接口
        :param id: ID
        :param parent_id: 父ID
        :param name: 菜单名称
        :param icon: 图标
        :param extend: 扩展数据
        """
        self.id: any = id
        self.parent_id: any = parent_id
        self.name: str = name
        self.extend: object = extend
        # 实例化
        self.treeWidgetItem = QTreeWidgetItem([self.name])
        # 存储相关数据
        self.treeWidgetItem.setData(0, Qt.UserRole + 1, extend)
        self.treeWidgetItem.setIcon(0, QIcon(':/icons/default.png'))
        if icon is not None:
            self.treeWidgetItem.setIcon(0, icon)


class ElTreeData(QObject):
    """
    Data Model
    """
    items_changed: Signal = Signal(str)
    styleSheet_changed: Signal = Signal(str)

    def __init__(self, items: list[TreeWidgetItem] = None, styleSheet: str = None):
        super(ElTreeData, self).__init__()
        # 定义数据
        self._items: list[TreeWidgetItem]
        self._styleSheet: str
        # 初始化数据
        self.items = items
        self.styleSheet = styleSheet

    @property
    def items(self):
        return self._items

    @items.setter
    def items(self, value):
        self._items = value
        # 数据改变时发出信号
        self.items_changed.emit(self.items)

    @property
    def styleSheet(self):
        return self._styleSheet

    @styleSheet.setter
    def styleSheet(self, value):
        self._styleSheet = value
        # 数据改变时发出信号
        self.styleSheet_changed.emit(self.styleSheet)


# PY PUSH BUTTON
# ///////////////////////////////////////////////////////////////
class PyTree(QTreeWidget):
    itemClicked_ = Signal(object)
    itemDoubleClicked_ = Signal(object)

    def __init__(
            self,
            radius=8,
            border_size=2,
            color="#FFF",
            selection_color="#FFF",
            bg_color="#333",
            bg_color_active="#222",
            context_color="#00ABE8",
            font=''
    ):
        super().__init__()
        # PARAMETERS
        self.setColumnCount(1)
        self.setHeaderHidden(True)
        # SET STYLESHEET
        self.set_stylesheet(
            radius,
            border_size,
            color,
            selection_color,
            bg_color,
            bg_color_active,
            context_color,
            font
        )
        self.itemClicked.connect(lambda item: self.itemClicked_.emit(item.data(0, Qt.UserRole + 1)))
        self.itemDoubleClicked.connect(lambda item: self.itemDoubleClicked_.emit(item.data(0, Qt.UserRole + 1)))

    # SET STYLESHEET
    def set_stylesheet(
            self,
            radius,
            border_size,
            color,
            selection_color,
            bg_color,
            bg_color_active,
            context_color,
            font
    ):
        # APPLY STYLESHEET
        style_format = style.format(
            _radius=radius,
            _border_size=border_size,
            _color=color,
            _selection_color=selection_color,
            _bg_color=bg_color,
            _bg_color_active=bg_color_active,
            _context_color=context_color,
            _font=font
        )
        self.setStyleSheet(style_format)

    def set_items(self, is_clear: bool, treeData: ElTreeData):
        if is_clear:
            self.clear()
        self.setColumnCount(1)
        self.setHeaderHidden(True)
        if treeData.items is not None:
            # 转为字典
            mapping: dict[any, TreeWidgetItem] = dict(zip([i.id for i in treeData.items], treeData.items))
            # 树容器
            treeWidgetItems: list[QTreeWidgetItem] = []
            for d in treeData.items:
                # 如果找不到父级项，则是根节点
                parent: TreeWidgetItem = mapping.get(d.parent_id)
                if parent is None:
                    treeWidgetItems.append(d.treeWidgetItem)
                else:
                    parent.treeWidgetItem.addChild(d.treeWidgetItem)
            # 挂载到树上
            self.insertTopLevelItems(0, treeWidgetItems)
